package com.prig.base.classManger.single.service;

import com.prig.base.DTO.input.PageKeywordSortDto;
import com.prig.base.classManger.single.entity.Single;
import com.prig.base.classManger.single.repository.SingleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author lww
 * @Description //TODO
 * @Date 2020/11/18 16:18
 **/
@Service
public class SingleSevice {
    @Autowired
    private SingleRepository singleRepository;

    public Page<Single> getList(PageKeywordSortDto dto) {
        //排序参考
//        List<Sort.Order> orders = new ArrayList<>();
//        orders.add(new Sort.Order(Sort.Direction.ASC,"parentId"));
//        orders.add(new Sort.Order(Sort.Direction.ASC,"parentIds"));
//        orders.add(new Sort.Order(Sort.Direction.ASC,"rank"));
//        Pageable pageable = PageRequest.of(dto.getSQLPage(),dto.getLimit(),Sort.by(orders));
        //关键字搜索参考
//        if (StrUtil.isNotBlank(dto.getKeyword())){
//            dto.setKeyword("%"+dto.getKeyword()+"%");
//            return permissionRepository.findByNameLike(dto.getKeyword(),pageable);
//        }
        //关键字编码型参考
//        if(StrUtil.isNotBlank(dto.getKeyword())){
//            return adminRepository.findAll(((root, criteriaQuery, criteriaBuilder) -> {
//                List<Predicate> predicateList = new ArrayList<>();
//                predicateList.add(
//                        criteriaBuilder.and(
//                                criteriaBuilder.like(
//                                        root.get("account"),"%"+dto.getKeyword()+"%"
//                                )
//                        )
//                );
//                return criteriaBuilder.and(
//                        predicateList.toArray(new Predicate[predicateList.size()])
//                );
//            }),pageable);
//        }
        Pageable pageable = PageRequest.of(dto.getSQLPage(),dto.getLimit(),dto.getSort());
        return singleRepository.findAll(pageable);
    }

    public Single add(Single single) {
        return singleRepository.save(single);
    }

    public void delete(Long id){
        singleRepository.deleteById(id);
    }
    public Single edit(Single single){
        return singleRepository.save(single);
    }

    public void delete(@NotBlank String ids) {

        String[] idsStr = ids.split(",");

        if (idsStr.length == 1){
            delete(Long.parseLong(idsStr[0]));
            return;
        }

        List<Long> idsLong = Arrays.asList(idsStr)
                .stream()
                .map(str -> Long.parseLong(str))
                .collect(Collectors.toList());
        singleRepository.deleteByIds(idsLong);
    }

    public Single get(long id) {
        return singleRepository.getOne(id);
    }
}
